﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class BorrowingDetailController : Controller
    {
        Library_BorrowingDetails libBorDetails = new Library_BorrowingDetails();


        [HttpPost]
        public JsonResult GetBorrowingDetailList(int ID = 0)
        {
            try
            {
                List<BorrowingDetailModel> borrowDetalList = libBorDetails.GetDataList(ID, 0, 0, "BookID ASC");
                return Json(new { Result = "OK", Records = borrowDetalList, TotalRecordCount = borrowDetalList.Count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddBorrowingDetail(BorrowingDetailModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                var add = libBorDetails.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }


        [HttpPost]
        public JsonResult UpdateBorrowingDetail(BorrowingDetailModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libBorDetails.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteBorrowingDetail(int Id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libBorDetails.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

    }
}
